/*
 * @Date: 2025-06-13 10:50:10
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-09-24 15:10:22
 * @FilePath: \corpus\src\pages\resourceManagement\fileList\component\CreateCorpus\index.tsx
 */
import MdEditor from '@/components/Priview/md';
import { useModel } from '@umijs/max';
import { Form, Modal, Radio, Select } from 'antd';
import type React from 'react';
import { useEffect } from 'react';
import hooks from '../../hooks';

const EditAngent: React.FC = () => {
  const {
    setCreateCorpusVisible,

    addCorpusFileContent,
    createCorpusVisible,
    corpusForm,
    corpusList,
    getCorpusBase,
    getPromptContent,
    selectedRowKeys,
  } = hooks.useContainer();
  const title = '生成语料库';

  const handleSubmit = () => {
    corpusForm.validateFields().then((vals) => {
      const method = addCorpusFileContent;
      method.run({
        ...vals,
        fileIds: selectedRowKeys,
      });
    });
  };

  const { setMdVisible, type, setType, setJsonData } = useModel('useTree', (model) => {
    return {
      setMdVisible: model.setMdVisible,
      setViewVisible: model.setViewVisible,
      setJsonData: model.setJsonData,
      type: model.type,
      setType: model.setType,
    };
  });

  useEffect(() => {
    if (createCorpusVisible) {
      getCorpusBase.run({
        current: 1,
        pageSize: 100,
      });
    } else {
      corpusForm.resetFields();
    }
  }, [createCorpusVisible]);
  return (
    <>
      <Modal
        width={700}
        title={title}
        open={createCorpusVisible}
        onCancel={() => {
          corpusForm.resetFields();
          setCreateCorpusVisible(false);
        }}
        onOk={handleSubmit}
        confirmLoading={addCorpusFileContent?.loading}
      >
        <Form labelCol={{ span: 7 }} form={corpusForm}>
          <Form.Item
            label={'语料库名称'}
            name="corpusBaseId"
            rules={[{ required: true, message: '请选择' }]}
          >
            <Select
              style={{ width: '100% ' }}
              placeholder="请选择"
              fieldNames={{ label: 'name', value: 'id' }}
              options={corpusList}
            />
          </Form.Item>
          <Form.Item
            label={`是否运用文本拆分能力`}
            name="isTextChunk"
            rules={[{ required: true, message: '请选择' }]}
          >
            <Radio.Group
              onChange={(val) => {}}
              options={[
                { label: '是', value: true },
                { label: '否', value: false },
              ]}
            />
          </Form.Item>
          <Form.Item label={'系统默认提示词'} name="textPrompt">
            <a
              onClick={async () => {
                // const res = await getPrompt('text');
                const res = await getPromptContent?.runAsync('text');
                if (corpusForm.getFieldValue('textPrompt')) {
                  setJsonData(corpusForm.getFieldValue('textPrompt'));
                } else {
                  setJsonData(res);
                }

                setType('text');
                setMdVisible(true);
              }}
            >
              修改文本拆分提示词
            </a>
          </Form.Item>
          <Form.Item
            label={`是否运用问答对能力`}
            name="isTextQa"
            rules={[{ required: true, message: '请选择' }]}
          >
            <Radio.Group
              onChange={(val) => {}}
              options={[
                { label: '是', value: true },
                { label: '否', value: false },
              ]}
            />
          </Form.Item>
          <Form.Item label={'系统默认提示词'} name="qaPrompt">
            <a
              onClick={async () => {
                // const res = await getPrompt('text');
                const res = await getPromptContent?.runAsync('qa');
                if (corpusForm.getFieldValue('qaPrompt')) {
                  setJsonData(corpusForm.getFieldValue('qaPrompt'));
                } else {
                  setJsonData(res);
                }
                setType('qa');
                setMdVisible(true);
              }}
            >
              修改问答对提示词
            </a>
          </Form.Item>

          <Form.Item label={''} name="" rules={[{ required: false, message: '请选择' }]}>
            <div style={{ padding: '10.0px 53.0px' }}>
              <p style={{ padding: '0px', color: 'gray' }}>
                1、运用语料治理能力为“否”时只会归集文件。
              </p>
              <p style={{ padding: '0px', color: 'gray' }}>
                2、修改文本拆分、问答对提示词可能会导致文本拆分、问答对生成失败，请谨慎修改。
              </p>{' '}
              <p style={{ padding: '0px', color: 'gray' }}>
                3、文本拆分支持格式：txt、json、jsonl。
              </p>
            </div>
          </Form.Item>
        </Form>
      </Modal>
      <MdEditor
        onChange={(a) => {
          if (type === 'text') {
            console.log('text');
            corpusForm.setFieldValue('textPrompt', a);
          } else {
            console.log('qa');
            corpusForm.setFieldValue('qaPrompt', a);
          }
        }}
      />
    </>
  );
};

export default EditAngent;
